﻿using System;
namespace Kis.Xroad.Eu {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="InlineComplexTypeArray", Namespace="http://kis.x-road.eu/", Target=typeof(InlineComplexTypeArray))]
    public class InlineComplexTypeArraySerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected InlineComplexTypeArray clazz;
        
        public InlineComplexTypeArraySerializer(InlineComplexTypeArray clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXmlAttributes(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXmlAttributes(Writer);
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("ManagedRegions");
            if (clazz.ManagedRegions!=null) {
                for (int x = 0; ((clazz.ManagedRegions != null) 
                            && (x < clazz.ManagedRegions.Count)); x = (x + 1)) {
                    if (clazz.ManagedRegions[x]!=null) {
                        Writer.WriteStartElement("item");
                        global::Xtee.Core.SerializationMetadata metaManagedRegions = this.GetMetaData(clazz.ManagedRegions[x]);
                        if (ShouldWriteTypeAttribute(typeof(InlineComplexTypeArray.ManagedRegionsType), clazz.ManagedRegions[x].GetType())) {
                            Writer.WriteTypeAttribute(metaManagedRegions.TypeQualifiedName.Namespace, metaManagedRegions.TypeQualifiedName.Name);
                        }
                        var managedRegionsSerializer = metaManagedRegions.NewSerializer(clazz.ManagedRegions[x]);
                        managedRegionsSerializer.WriteXmlAttributes(Writer);
                        managedRegionsSerializer.WriteXml(Writer);
                        Writer.WriteEndElement();
                    }
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : ManagedRegions");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXmlAttributes(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXmlAttributes(Reader);
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="ManagedRegions") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>InlineComplexTypeArray>ManagedRegions");
                }
                clazz.ManagedRegions = new System.Collections.Generic.List<InlineComplexTypeArray.ManagedRegionsType>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("ManagedRegions");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "item")); ) {
                        if ((Reader.IsElementNil() == false)) {
                            global::Xtee.Core.SerializationMetadata metaManagedRegions = this.GetMetaData<InlineComplexTypeArray.ManagedRegionsType>(Reader, ">>>InlineComplexTypeArray>ManagedRegions>item", "http://kis.x-road.eu/");
                            InlineComplexTypeArray.ManagedRegionsType item = metaManagedRegions.NewEntity<InlineComplexTypeArray.ManagedRegionsType>();
                            clazz.ManagedRegions.Add(item);
                            if ((Reader.IsEmptyElement == false)) {
                                var managedRegionsSerializer = metaManagedRegions.NewSerializer(item);
                                managedRegionsSerializer.ReadXmlAttributes(Reader);
                                Reader.ReadStartElement("item");
                                managedRegionsSerializer.ReadXml(Reader);
                                Reader.ReadEndElement();
                            }
                            else {
                                Reader.Skip();
                            }
                        }
                        else {
                            clazz.ManagedRegions.Add(null);
                            Reader.Skip();
                        }
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>InlineComplexTypeArray>ManagedRegions", Reader.LocalName);
            }
        }
        
        [global::Xtee.Core.Attributes.XmlTypeAttribute(Name=">>>InlineComplexTypeArray>ManagedRegions>item", Namespace="http://kis.x-road.eu/", Target=typeof(InlineComplexTypeArray.ManagedRegionsType))]
        public class ManagedRegionsTypeSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
            
            protected InlineComplexTypeArray.ManagedRegionsType clazz;
            
            public ManagedRegionsTypeSerializer(InlineComplexTypeArray.ManagedRegionsType clazz) {
                this.clazz = clazz;
            }
            
            public override void WriteXmlAttributes(global::Xtee.Core.IXteeXmlWriter Writer) {
                base.WriteXmlAttributes(Writer);
            }
            
            public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
                base.WriteXml(Writer);
                Writer.WriteStartElement("Country");
                if (clazz.Country!=null) {
                    Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.Country))).ToString());
                }
                else {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Country");
                }
                Writer.WriteEndElement();
                Writer.WriteStartElement("City");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdInt(((int)(clazz.City))).ToString());
                Writer.WriteEndElement();
                Writer.WriteStartElement("Street");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdInt(((int)(clazz.Street))).ToString());
                Writer.WriteEndElement();
            }
            
            public override void ReadXmlAttributes(global::Xtee.Core.IXteeXmlReader Reader) {
                base.ReadXmlAttributes(Reader);
            }
            
            public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
                base.ReadXml(Reader);
                if (Reader.LocalName=="Country") {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>>>InlineComplexTypeArray>ManagedRegions>item>Country");
                    }
                    clazz.Country = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("Country")).Value;
                }
                else {
                    throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>>InlineComplexTypeArray>ManagedRegions>item>Country", Reader.LocalName);
                }
                if (Reader.LocalName=="City") {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>>>InlineComplexTypeArray>ManagedRegions>item>City");
                    }
                    clazz.City = new global::Xtee.Core.Schema.XsdInt(Reader.ReadElementString("City")).Value;
                }
                else {
                    throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>>InlineComplexTypeArray>ManagedRegions>item>City", Reader.LocalName);
                }
                if (Reader.LocalName=="Street") {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>>>InlineComplexTypeArray>ManagedRegions>item>Street");
                    }
                    clazz.Street = new global::Xtee.Core.Schema.XsdInt(Reader.ReadElementString("Street")).Value;
                }
                else {
                    throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>>InlineComplexTypeArray>ManagedRegions>item>Street", Reader.LocalName);
                }
            }
        }
    }
}
